package com.sllp.C71S3TsllpShop.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.ProductExample;
import com.sllp.C71S3TsllpShop.bean.ProductExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.ProductPriceRange;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.FavoriteBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.ProductBizImpl;
import com.sllp.C71S3TsllpShop.dao.ProductMapper;

@Controller
public class CategoryListAction {

	@Resource
	private ProductBizImpl pbiz;
	@Resource
	private ProductMapper pm;
	@Resource
	private CarBizImpl cbiz;
	@Resource
	private FavoriteBizImpl fbi;
	
	//添加收藏
		@ResponseBody
		@PostMapping("addToMyFav.do")
		public Result addToMyFav(Favorite favorite,@SessionAttribute("loginUser") User user) {
			favorite.setUser(user.getId());
			Result i = fbi.addToMyFav(favorite);
			return i;
		}

	/**
	 * 
	 * @param id 小类型ID
	 * @param m
	 * @return
	 */
	@GetMapping({"categorylist","categorylist.do"})
	public String toCategoryList(HttpSession session,
			@SessionAttribute("btlist") List<List<BigType>> lists,
			@SessionAttribute("loginUser") User user,
			String pre,String next, int id,Model m) {
		m.addAttribute("btlist", lists);
		
		Product product2 = new Product();
		product2.setType(id);
		List<Product> sbst = pbiz.findProductByCondition(product2, null, null, 1, 2);
		
		// 获取最高价
		BigDecimal maxPrice=new BigDecimal(0);
		
		// 店铺去重
		List<Business> businesss=new ArrayList<Business>();
		List<Favorite> favoriteList = fbi.findfavorite(user);
		Business business=null;
		for (Product product : sbst) {
			//添加收藏
			for (int i=0; i<favoriteList.size();i++) {
				if(product.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==2) { 
					product.setIsFav(1);
					break;
				}else if(product.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==1) {
					product.setIsFav(0);
					break;
				} else if(product.getId()!=favoriteList.get(i).getProduct() && i==favoriteList.size()-1){
					product.setIsFav(0);
				}
			}
			if(maxPrice.compareTo(product.getMarketPrice())==-1) {
				maxPrice=product.getMarketPrice();
			}
			//店铺去重
			if(businesss.size() > 0) {
				if(business.getId() != product.getBusiness2().getId()) {
					business=product.getBusiness2();
					for(int i = 0; i < businesss.size(); i++) {
						if(businesss.get(i).getId() == business.getId()) {
							break;
						}else if(businesss.get(i).getId() != business.getId() && i==businesss.size()-1) {
							businesss.add(business);
						}
					}
				}
			}else {
				business=product.getBusiness2();
				businesss.add(business);
			}
		}
		m.addAttribute("businessList", businesss);
		
		// 获取条件查询中的价格区间
		List<ProductPriceRange> productPriceRanges = new ArrayList<ProductPriceRange>();
		int maxPriceInt=maxPrice.intValue();
		for(int i=0; i<7;i++) {
			ProductPriceRange productPriceRange = new ProductPriceRange(new BigDecimal((maxPriceInt/7)*i), new BigDecimal((maxPriceInt/7)+(maxPriceInt/7)*i));
			productPriceRanges.add(productPriceRange);
		}
		m.addAttribute("productPriceRanges", productPriceRanges);
		System.out.println(productPriceRanges);
		
		//System.out.println(businesss);
		m.addAttribute("product",sbst);
		int i = sbst.size();
		m.addAttribute("number",i);
		//System.out.println(sbst);
		List<Product> sbb = pm.selectByBuyNum1(id);
		List<Product> list = new ArrayList<>();
		if(sbb.size()>=3) {
			for(int j=0;j<3;j++) {
				list.add(sbb.get(j));
			}
		}else {
			list.addAll(sbb);
		}
		//System.out.println(list);
		m.addAttribute("sbblist",list);
//		System.out.println(sbst);
		return "categorylist";
	}
	
	@PostMapping("categoryProdictList.do")
	public String findByPrice(@SessionAttribute("loginUser") User user,
			Integer marketPrePrice,Integer marketNextPrice,Integer businessId,Integer id,Integer page,Model m) {
		Product product = new Product();
		product.setType(id);
		product.setBusiness(businessId);
		List<Product> sbbn = pbiz.findProductByCondition(product, marketPrePrice, marketNextPrice, page, 2);
		List<Favorite> favoriteList = fbi.findfavorite(user);
		for (Product product1 : sbbn) {
			//添加收藏
			for (int i=0; i<favoriteList.size();i++) {
				if(product1.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==2) { 
					product1.setIsFav(1);
					break;
				}else if(product1.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==1) {
					product1.setIsFav(0);
					break;
				} else if(product1.getId()!=favoriteList.get(i).getProduct() && i==favoriteList.size()-1){
					product1.setIsFav(0);
				}
			}
		}
		m.addAttribute("product",sbbn);
		System.out.println(sbbn);
		return "categorylist::categoryProductList";
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
